<template>
	<view>
		<div>
			<navitop>
				<i class="iconfont" slot="left-slot" @click="outlot">&#xe667;</i>
				<span slot="center-slot">新增客户信息</span>
				<i class="iconfont " slot="right-slot" @click="add"></i>
			</navitop>
		</div>
		<div  :style="'margin-top:' + nv_height"></div>
		<scroll-view scroll-y="true" touch-action=none; >
		<div class="detial" >
			<div class="public_list_style" v-for="(item,index) in dataSorce">
				<div class="public_list_out" v-if="item.onEdit">
					<div class="public_list" v-if="item.code !== 'areaId' && item.code !== 'crmNo' && item.type !== '下拉' && item.type !== '搜索选择' && item.type !== '时间'  && item.type !== '日期'">
						<div class="public_list_left">
							<text v-if="item.onAppFill" style="color: red; ">*</text>
							<text>{{item.name}}:</text>						 
						</div>
						<div class="public_list_right">
							<input type="text" v-model="item.value"  >					 
						</div>	
						
					</div>
					<div class="public_list" v-else-if="item.code === 'areaId'">
						<div class="public_list_left">
							<text v-if="item.onAppFill" style="color: red;">*</text>
							<text>{{item.name}}:</text>
						</div>
						<div class="public_list_right">
							<input type="text" disabled='true'  @click="alertcity" v-model="item.value"  >
						</div>
					</div>
					<div class="public_list" v-else-if="item.type === '下拉'">
						<div class="public_list_left">
							<text v-if="item.onAppFill" style="color: red;">*</text>
							<text>{{item.name}}:</text>
						</div>
						<div class="public_list_right">
							<input type="text" disabled='true'  @click="drop_duon(item.code)" v-model="item.value"  >
						</div>
					</div>
					<div class="public_list" v-else-if="item.type === '搜索选择'">
						<div class="public_list_left">
							<text v-if="item.onAppFill" style="color: red;">*</text>
							<text>{{item.name}}:</text>
						</div>
						<div class="public_list_right">
							<input type="text" disabled='true'  @click="drop_duo(item.code)" v-model="item.value"  >
						</div>
					</div>
					<div class="public_list" v-else-if="item.type === '时间' || item.type === '日期'" @click="time_dom(item.code)">
						<div class="public_list_left">
							<text v-if="item.onAppFill" style="color: red;">*</text>
							<text>{{item.name}}:</text>
						</div>
						<div class="public_list_right">
							<kxTime :date="item.value" style="z-index: 1;" :end='enddate' :start='startdate' @rundata='lasttime'  default='start' placeholder="选择时间"></kxTime> 
						</div>
					</div>
					
				</div>
			</div>
		</div>
		</scroll-view>
		<div :style="heigsdiv"></div>
		<div class="public_button" :style="heigs"> 
			<div class="public_text" @click="add"> 
				保&nbsp;&nbsp;存
			</div>
		</div>
		<!-- 弹窗组件 -->
		<popus ref="alerts">
			<alertcenter @row="row" @closes="closes" :dataSorc="dataSo" ></alertcenter>
		</popus>
		<ztree ref='alert' @cityalerts="cityalerts" :dataSorce="citys_c"></ztree>
	</view>
</template>

<script>
	import navitop from '@/components/common/navi-top'
	import kxTime from "@/components/kx-datetime/kx-datetime"
	import alertcenter from '@/pages/common/common/alert/alertcenter'
	import ztree from '@/pages/common/common/city/z_citys.vue'
	import popus from '@/uni_modules/uni-popup/components/uni-popup/uni-popup'
	import {detail,save,namelist,city,list,dataList,pages} from '@/common/api/job/jobapi.js'
	import {droplist} from '@/common/api/job/alert.js'
	import {Z_threes} from '@/common/api/common/public_api.js'
	export default {
		components:{
			navitop,
			popus,
			alertcenter,
			ztree,
			kxTime
		},
		data(){
			return {
				dataSorce: [],
				results:[],
				citys_c: [],
				crmType: '',
				data:[],
				date: '', //需要展示的时间数据
				enddate: "2200", // 时间段的终止时间，不设置默认当前之间终止
				startdate: "2021" ,//时间段的开始时间，不设置默认计算机最初时间
				crmName: '',
				street:'',
				areaName:'',
				enterpriseScale: '',
				url:'',
				email: '',
				mobile: '',
				phone: '',
				industry: '',
				type:'',
				crmSource: '',
				uid: '',
				remarks:'',
				drop_down:[],
				crmGrade:'',
				registerCapital: '',
				legalPerson:'',
				class_name: '',
				uid: '',
				heigs: '',
				heigsdiv: 0,
				zheight: '',
				zheights: '',
				jianpan: '',
				jianpans: '',
				objck: '',
				dataSo: []	 ,//弹窗数据
				uplist: [],		// 获取下拉列表
				// 获取部门
				dataLi: [],
				public_name:[],
				lastTim:'',
				set_code: '',// 获取时间code
				nv_height: '',
				
			}
		},
		onLoad(option) {
			
			let _this = this;
			this.crmType = option.crmType;
			this.class_name = option.name;
			this.uid = option.id;
			this.listdata();
			this.citys();
			this.up_list();
			this.dataLis();

		
			uni.onWindowResize((res) => {
				this.jianpan = res.size.windowHeight
			    console.log('变化后的窗口宽度=' + res.size.windowWidth)
			    console.log('变化后的窗口高度=' + res.size.windowHeight)
			})
	
			this.nv_height = uni.getSystemInfoSync().statusBarHeight + 75 + 'px';
			
			uni.getSystemInfo({
				success: function(res) {
					_this.jianpans = res.screenHeight;
					_this.zheight = res.windowHeight;
					// console.log(res.screenHeight); //屏幕高度  注意这里获得的高度宽度都是px 需要转换rpx
					// console.log(res.windowHeight); //可使用窗口高度
				}
			})
		
		},

		mounted() {
		uni.onKeyboardHeightChange(res => {
			
			
			res.height = res.height + 50;
			this.heigsdiv = 'height:' + res.height  + 'px;' ;
			this.heigs = 'height:' + res.height + 'px;' ;
		})
		},
		methods:{
			outlot(){
				uni.navigateTo({
				    url: '/pages/common/index/second?id=' + this.uid +'&name=' + this.class_name + '&crmType=' + this.crmType
				});
			},
		
			open() {
			     this.$refs.alerts.open('bottom')
			},
			closes(){
				this.$refs.alert.close();
			},
			
			alertcity(){
				this.$refs.alert.z_three();
			},
			// 地区数据回显
			cityalerts(val){
				this.dataSorce.forEach(item => {
					if(item.code == 'areaId'){
						this.$set(item,'value',val)
					}
				})
			},
			// 获取地区
			citys(){
				let children = [];
				city({current: 1,size: -1}).then((result)=> {
					this.citys_c = Z_threes(result.data,[])
				})
			},
		
			row(val){
				this.dataSorce.forEach(item => {
					if(item.code == val.code){
						this.$set(item,'value',val.name)
					}
				})
				this.$refs.alerts.close();
			},
			// 下拉
			drop_duon(val){
				let rst = [];
				for(let i in this.uplist){
					if(val == this.uplist[i].code){
						rst = droplist(this.uplist[i],val);
					}
				}
				this.dataSo = rst;
				this.$refs.alerts.open('bottom')
			},
			// 搜索
			drop_duo(val){
				if(val == 'officeId'){
					this.dataLi.forEach(item =>{
						this.$set(item,'code',val)
					})
					this.dataSo = this.dataLi;
					this.$refs.alerts.open('bottom')
				}else{
					this.public_name.forEach(item =>{
						this.$set(item,'code',val)
					})
					this.dataSo = this.public_name;
					this.$refs.alerts.open('bottom')
				}
			},
			
			// 获取下拉列表
			up_list(){
				list({source: 'crm'}).then((rows)=>{
					if(rows.code == 200){
						this.uplist = rows.data;
					}
				})
			},
				// 获取部门
			dataLis(){
				dataList({isLoadUser: 2}).then((rows) => {
					if(rows.code == 200){
						this.public_name = rows.data;
						for(let i in rows.data){
							if(rows.data[i].parentId !== '0' && rows.data[i].id.split("_")[1] !== 'user'){
								this.dataLi.push(rows.data[i]);
							}
						}
					}
				})
			},
			
			// 时间处理
			time_dom(val){
				this.set_code = val
				console.log(val)
			},
			
			lasttime(e){
				this.lastTim =e;
				this.dataSorce.forEach(item => {
					if(item.code == this.set_code){
						this.$set(item,'value',e);
					}
				})
			},
			
			add(){
				let opk = {};
				let custom = [];
				let pas = {};
				let stalarray = [];
				
				for(let i in this.dataSorce){
					if(this.dataSorce[i].onAppFill){
						if(!this.dataSorce[i].value){
							uni.showToast({
								title: '请输入' + this.dataSorce[i].name,
								icon: 'error'
							})
						return false;
						}else{
							if(this.dataSorce[i].code == 'areaId'){
								opk['areaName'] = this.dataSorce[i].value
							}else{
								opk[this.dataSorce[i].code] = this.dataSorce[i].value
							}
						}
					}else {
						if(this.dataSorce[i].code == 'areaId'){
							opk['areaName'] = this.dataSorce[i].value
						}else{
							opk[this.dataSorce[i].code] = this.dataSorce[i].value
						}
					}
				}
				opk['crmType'] = this.crmType;
				// defineFields: "{}"
				delete opk.crmNo
				this.dataSorce.forEach(item => {
					if(item.dataType == 2){
						pas[item.id] = item.value
					}
				})
				opk['defineFields'] = JSON.stringify(pas);
				opk['isCheck'] = true;
				// opk["crmNo"] = '';
				console.log(opk);
				save(opk).then((rst) => {
					if(rst.code == 200){
						uni.showToast({
						    title: '添加成功！',
						});
						
						list({'source': 'crm'}).then((row) => {
							if(row.code == 200){
							pages({'dtos':row.data,'crmType': this.crmType,'customerIds':'','isCollected':false,page: {current: 1, size: 100}}).then((val) => {
								if(val.code == 200){  
									stalarray = val.data.records[0]
								}
							})
							}
						})
						setTimeout(() => {
							uni.navigateTo({
							    url: '/pages/common/index/detial?id=' + stalarray.id +'&name=' + this.class_name + '&crmType=' + this.crmType + '&classid=' + this.uid + '&index=' + 0
							});
						},1500)
					}
				})
			},
		
			// 获取列表
			listdata(){
				var stringtable = [];
				var stringvalue = [];
				var sows = [];
				list({source: 'crm'}).then((row) => {
					if(row.code == 200){
						for(let i in row.data){
							row.data[i]['val'] = '';
							row.data[i]['value'] = '';
						}
						this.dataSorce = row.data.splice(1,);
					}
				})
			},
		
		}
	}
	
</script>

<style scoped lang="scss">

</style>
